﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_AssemblyCheckType : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_AssemblyCheckType()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.AssemblyCheckType.DoubleClick += AssemblyCheckType_DoubleClick;
            this.AssemblyCheckType.PopupMenuShowing += AssemblyCheckType_PopupMenuShowing;
            this.assemblyCheckTypeContextMenu.ItemClicked += assemblyCheckTypeContextMenu_ItemClicked;
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            DataRow row = AssemblyCheckType.GetFocusedDataRow();
            frm_Edit_AssemblyCheckType editForm = new frm_Edit_AssemblyCheckType();
            editForm.id = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                editForm.Dispose();
                MessageBox.Show("Cannot edit record.");
            }
        }

        void assemblyCheckTypeContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.assemblyCheckTypeContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_AssemblyCheckType add_Form = new frm_Add_AssemblyCheckType();
                    add_Form.ShowDialog();
                    LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void AssemblyCheckType_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.assemblyCheckTypeContextMenu.Show(MousePosition);
        }

        private void AssemblyCheckType_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT * FROM [231_CheckType] ORDER BY CheckTypeName";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridAssemblyCheckType.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("CheckTypeID", typeof(int));
                newTable.Columns.Add("CheckTypeName", typeof(string));                
                gridAssemblyCheckType.DataSource = newTable;
            }
            AssemblyCheckType.Columns[0].Caption = "Check Type ID";
            AssemblyCheckType.Columns[1].Caption = "Check Type Name";            
            AssemblyCheckType.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_AssemblyCheckType add_Form = new frm_Add_AssemblyCheckType();
            add_Form.ShowDialog();
            LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        private void bottonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
